<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="stuName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住院号" prop="inpatientNo">
        <el-input
          v-model="queryParams.inpatientNo"
          placeholder="请输入住院号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出院日期" prop="effectiveE">
        <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['hospital:synthetical:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examinationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="住院号" align="center" prop="inpatientNo" />
      <el-table-column label="住院次数" align="center" prop="admissTimes" />
      <el-table-column label="病区名称" align="center" prop="wardName" />
      <el-table-column label="床号" align="center" prop="bedNo" />
      <el-table-column label="医保身份" align="center" prop="respName" />
      <el-table-column label="结算状态" align="center" prop="settleName" />
      <el-table-column label="出院时间" align="center" prop="effectiveE" >
        <template #default="scope">
          {{ dataFliter(scope.row.effectiveE) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)" v-hasPermi="['hospital:synthetical:detail']">详情</el-button>
          <el-button link type="primary" v-if="scope.row.emrWeb != null" @click="handleUrl(scope.row)" v-hasPermi="['hospital:synthetical:url']">病历</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <a ref="target" href="" target="_blank"></a>
    <!--详情信息对话框-->
    <el-dialog title="详情" v-model="detailOpen" width="1000px" append-to-body>
        <el-form :model="patientForm" label-width="100px">
          <el-form-item label="姓名：" prop="name">
            {{patientForm.name}}
          </el-form-item>
          <el-form-item label="住院号：" prop="inpatientNo">
            {{patientForm.inpatientNo}}
          </el-form-item>
          <el-form-item label="住院次数：" prop="admissTimes">
            {{patientForm.admissTimes}}
          </el-form-item>
          <el-form-item label="病区名称：" prop="wardName">
            {{patientForm.wardName}}
          </el-form-item>
          <el-form-item label="科室名称：" prop="department">
            {{patientForm.disDeptName}}
          </el-form-item>
          <el-form-item label="床号：" prop="bedNo">
            {{patientForm.bedNo}}
          </el-form-item>
          <el-form-item label="医保身份：" prop="respName">
            {{patientForm.respName}}
          </el-form-item>
          <el-form-item label="结算状态：" prop="settleName">
            {{patientForm.settleName}}
          </el-form-item>
          <el-form-item label="出科时间：" prop="effectiveE">
            {{dataFliter(patientForm.effectiveE)}}
          </el-form-item>
        </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Examination">
import { selectSyntheticalList, detail } from "@/api/hospital/synthetical";
import dayjs from "dayjs"

const { proxy } = getCurrentInstance();
const examinationList = ref([]);
const detailOpen = ref(false)
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref([]);
const statusList = ref([{value:"0",label:"未开始"},{value:"1",label:"申请中"},{value:"2",label:"已完成"},{value:"3",label:"驳回"}])
const data = reactive({
  form: {},
  patientForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    name: null,
    inpatientNo: null,
    status: null,
  }
});

const { queryParams, patientForm } = toRefs(data);

function dataFliter(val,format = "YYYY-MM-DD hh:mm:ss"){
  return dayjs(val).format(format);
}

function stateFilter(state) {
  let txt = ""
  switch (state){
    case "0":
      txt = "未开始";
      break;
    case "1":
      txt = "申请中";
      break;
    case "2":
      txt = "已完成";
      break;
    case "3":
      txt = "驳回";
      break;
  }
  return txt
}

function genderFilter(state) {
  let txt = ""
  switch (state){
    case 1:
      txt = "男";
      break;
    case 2:
      txt = "女";
      break;
  }
  return txt
}

function formattedDate(time){
  const date = new Date(time);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const tem =  year + '-' + month + '-' + day
  return tem;
}

/** 查询报销列表 */
function getList() {
  loading.value = true;
  selectSyntheticalList(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    examinationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 详情取消按钮
function detailCancel() {
  detailOpen.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 详情按钮操作 */
function handleDetail(row) {
  detailOpen.value = true;
  detail(row.id).then( response =>{
    patientForm.value = response.data.patient;
  })
}
/** 超链接按钮操作 */
function handleUrl(row) {
  let target = proxy.$refs.target
  target.setAttribute('href', row.emrWeb)
  target.click()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('hospital/synthetical/export', {
    ...queryParams.value
  }, `综合查询_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style type="text/css">
.el-row {
  display: flex;
  flex-wrap: wrap
}
.el-col {
  border-radius: 4px;
  align-items: stretch;
}
.el-card {
  min-width: 100%;
  height: 100%;
  margin-right: 20px;
  /*transition: all .5s;*/
}
.el-card:hover{
  margin-top: -5px;
}
</style>
